package com.zyl.blog.controller.admin;
/*
 * @author 14323
 * @date 2020/8/8 13:24
 */

import com.zyl.blog.pojo.Blog;
import com.zyl.blog.pojo.User;
import com.zyl.blog.service.BlogService;
import com.zyl.blog.service.TagService;
import com.zyl.blog.service.TypeService;
import com.zyl.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2020年08月08日 13:24
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogs(@PageableDefault(
            size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blogQuery, Model model) {

        System.out.println(blogQuery.getTitle()+blogQuery.getTypeId()+blogQuery.isRecommend());
        model.addAttribute("types", typeService.getAllTypes());
        model.addAttribute("page", blogService.getBlogPage(pageable, blogQuery));
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(
            size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blogQuery, Model model) {

        System.out.println(blogQuery);
        model.addAttribute("page", blogService.getBlogPage(pageable, blogQuery));
        return "admin/blogs :: bloglist";
    }

    @GetMapping("/blogs/input")
    public String input(Model model) {
        model.addAttribute("tags", tagService.getAllTags());
        model.addAttribute("types", typeService.getAllTypes());
        model.addAttribute("blog", new Blog());
        return INPUT;
    }

    @GetMapping("/blogs/{id}/input")
    public String input(@PathVariable Long id, Model model) {
        model.addAttribute("tags", tagService.getAllTags());
        model.addAttribute("types", typeService.getAllTypes());
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog", blog);
        return INPUT;
    }

    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes) {

        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));

        Blog saveBlog;

        if(blog.getId() == null) {
            //新增博客
            saveBlog = blogService.saveBlog(blog);
        } else {
            //更新博客
            saveBlog = blogService.updateBlog(blog.getId(), blog);
        }

        if (saveBlog == null) {
            if (blog.getId() == null){
                attributes.addFlashAttribute("message", "新增失败！");
            } else {
                attributes.addFlashAttribute("message", "更新失败！");
            }
        } else {
           if (blog.getId() == null){
               attributes.addFlashAttribute("message", "新增成功！");
           } else {
               attributes.addFlashAttribute("message", "更新成功！");
           }
        }

        return REDIRECT_LIST;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功！");
        return REDIRECT_LIST;
    }

    @GetMapping("/index")
    public String index() {
        return "admin/index";
    }

}
